﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using He.Common;
using He.Web.UI;

namespace He.Web.tools
{
    /// <summary>
    /// compress_ajax 的摘要说明
    /// </summary>
    public class compress_ajax : AjaxHandler
    {
        He.Model.webset webset = new He.BLL.webset().GetWebset();
        public override void ProcessRequest(HttpContext context)
        {
            //检查是否登录后上传操作
            if (!new He.Web.UI.Manage().IsAdminLogin())
            {
                context.Response.Write("{\"msg\": \"0\", \"msbox\": \"请登录后再进行上传文件！\"}");
                return;
            }
            //是否开启图片压缩
            if (webset.attachimgmaxwidth <= 0 || webset.attachimgmaxheight <= 0)
            {
                EchoJson(0, "您还未开启图片压缩功能，请进入系统图片设置开启！");
                return;
            }

            string act = InputUtil.Get("act");

            switch (act)
            {
                case "all": //全部压缩
                    CompressAll();
                    break;
            }
        }

        #region 全部压缩===================================
        private void CompressAll()
        {
            int cid;
            if (!int.TryParse(InputUtil.Get("cid"), out cid))
            {
                return;
            }
            He.BLL.article bll = new BLL.article();
            DataTable dt = bll.GetList(cid, "", "Id desc").Tables[0];
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["ImgUrl"].ToString().Trim() != "" && dr["img_urls"].ToString().Trim() != "")
                {
                    string[] imgArr = dr["img_urls"].ToString().Split(',');
                    foreach (string imgurl in imgArr)
                    {
                        ImageHelper.CompressImage(imgurl, imgurl, webset.attachimgmaxwidth, webset.attachimgmaxheight, "max");
                    }
                }
            }
            EchoJson(1, "图片压缩成功！");
        }
        #endregion

        public override bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}